// 引入必要的模块
import _ from 'lodash'
import puppeteer from '../../lib/puppeteer/puppeteer.js';

export class wukong extends plugin {
    // 构造函数，初始化插件的属性
    constructor() {
        super({
            // 插件名称
            name: '悟空功能插件',
            // 插件描述
            dsc: '悟空功能插件',
            // 监听方式
            event: 'message',
            // 插件优先级
            priority: 5001,
            // 匹配正则
            rule: [
                {
                    /***匹配正则***/
                    reg: '^#影神图(.*)$',
                    /***执行方式***/
                    fnc: 'wkyst'
                },{
                    /***匹配正则***/
                    reg: '^#悟空攻略(.*)$',
                    /***执行方式***/
                    fnc: 'wkgl'
                },
            ]
        })

    }

    async wkyst(e) {
        let name = _.trimStart(e.msg, "#影神图")
        if (!puppeteer.browser) {
            await puppeteer.browserInit();
        }
        const page = await puppeteer.browser.newPage();
        await page.setViewport({ width: 400, height: 600 });
        let url = `https://wiki.biligame.com/wukong/${name}`;
        logger.info('url:',url)
        await page.goto(url,{ waitUntil: 'networkidle2', timeout: 30000 });
        let boxHeight= await page.$eval('.mw-content-ltr', el => el.clientHeight);
        let boxWidth = await page.$eval('.mw-content-ltr', el => el.clientWidth);
        let elemHandle = await page.$('.mw-content-ltr');
        let msg = [
            await segment.image( await elemHandle.screenshot({
                clip: {
                x: 2,
                y: 2,
                width: boxWidth,
                height: boxHeight
                }
            }))
        ];
        e.reply(msg);
        await page.close();
        return true;
    }
	
	async wkgl(e) {
        let name = _.trimStart(e.msg, "#悟空攻略")
        if (!puppeteer.browser) {
            await puppeteer.browserInit();
        }
        const page = await puppeteer.browser.newPage();
        await page.setViewport({ width: 660, height: 700 });
        if (!name) {
            e.reply('请输入关键字(酒|丹方|打坐|武器|护甲|珍玩|精魄|种子)');
            return true;
        }
        let code = '';
        let id = '';
        if (name == '酒') {
            code = '34992033';
            id = '#co-728';
        }
        if (name == '丹方') {
            code = '34992034';
            id = '#co-685';
        }
        if (name == '打坐') {
            code = '34992036';
            id = '#co-691';
        }
        if (name == '武器') {
            code = '34992030';
            id = '#co-748';
        }
        if (name == '护甲') {
            code = '34992031';
            id = '#co-781';
        }
        if (name == '珍玩') {
            code = '34992028';
            id = '#co-747';
        }
		if (name == '精魄') {
            code = '34992032';
            id = '#co-679';
        }
        if (name == '种子') {
            code = '34992035';
            id = '#co-704';
        }
        if (!code) {
            e.reply('没有该攻略');
            return true;
        }
        let url = `https://psn0.com/trophy/${code}`;
        logger.info('url:',url)
        await page.goto(url,{ waitUntil: 'networkidle2', timeout: 30000 });
        let boxHeight= await page.$eval(`${id}`, el => el.clientHeight);
        let boxWidth = await page.$eval(`${id}`, el => el.clientWidth);
        let elemHandle = await page.$(`${id}`);
        let msg = [
            await segment.image( await elemHandle.screenshot({
                clip: {
                x: -60,
                y: -60,
                width: boxWidth+100,
                height: boxHeight+100
                }
            }))
        ];
        e.reply(msg);
        //const image = await page.screenshot({fullPage: true })
        //e.reply(segment.image(image));
        await page.close();
        return true;
    }

}
